<?php  

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order extends CI_Controller {
	function __construct()
	{
	parent::__construct();
		
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}		
		$this->load->library('form_validation');
		$this->load->model('purchase_order/order_model');	
		$this->load->model('Purchase/purchase_model');
		$this->load->library('ion_auth');
		$this->load->library('session');
		$this->load->helper('url');
		$this->load->library('datatables');
		$this->config->item('use_mongodb', 'ion_auth') ?
		$this->load->library('mongo_db') :
        $this->load->database();

		$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));

	}
		
	function add_order(){
		//echo "i am ghere";exit; 
			if (!$this->ion_auth->logged_in())
			{
				redirect('auth', 'refresh');
			}
          $data=$this->order_model->get_order_id();
		  $code=$this->order_model->get_invoice_code();
		  $enquiry_code=$code['invoice_code']."/ORDER/";
		  $new_code=intval($data['id']+1);
		  $new_id=$enquiry_code.$new_code;
		  $this->form_validation->set_rules('view_date', 'Received date', 'required|xss_clean');
		  $this->form_validation->set_rules('expected_date', 'Expected Date', 'required|xss_clean');
		  $this->form_validation->set_rules('id_supplier', 'Supplier', 'required');
		  $this->form_validation->set_rules('order_no', 'Oreder_no', 'required|xss_clean');
		  $this->data['suppliers']=$this->order_model->get_supplier();
		  $meta['page_title'] = "Add Order";
	   		if (isset($_POST) && !empty($_POST))
		    { 
			$data = array(
				'order_no' => $this->input->post('order_no'),
				'id_supplier' => $this->input->post('id_supplier'),
				'order_date' => $this->input->post('order_date'),
				'expected_date' => $this->input->post('expected_date'),	
				'status'   =>strtoupper('pending'),
				'order_by' => strtoupper($this->session->userdata('username'))
			);
			//print_r($data);exit;
			if ($this->form_validation->run() === TRUE)
			{
				$latest_id = $this->order_model->insert($data); 
				$name = $this->input->post('name');
				$category = $this->input->post('category');
				$company = $this->input->post('company');
				$quantity = $this->input->post('quantity');
				$length = count($name);
					for($i=0; $i<$length; $i++){
						$new_data['id_order'] = $latest_id ;
						$new_data['name'] = $name[$i];
						$new_data['category'] = $category[$i];	
						$new_data['company'] = $company[$i];
						$new_data['quantity'] = $quantity[$i];
						$this->order_model->insert_item($new_data);	
						}							
				
				redirect("purchase_order/order/view_order");
			}
			
		}

		$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

		
 		$this->data['order_no'] = array(
			'name'  => 'order_no',
			'id'    => 'order_no',
			'type'  => 'text',
			'readonly'=> 'readonly',
			'value' => $this->form_validation->set_value('order_no',$new_id ),
		);

		
		$this->data['view_date'] = array(
			'name'  => 'view_date',
			'id'    => 'view_date',
			'type'  => 'text',
			'required' =>'required',
		'data-date-format' =>'yyyy/mm/dd',
			'value' => $this->form_validation->set_value('view_date'),
			
		);
		
		$this->data['expected_date'] = array(
			'name'  => 'expected_date',
			'id'    => 'expected_date',
			'type'  => 'text',
			'required' =>'required',
			'data-date-format' =>'yyyy/mm/dd',
			'value' => $this->form_validation->set_value('expected_date'),
		
		);

	  $this->data['id_supplier'] = array(
			'name'  => 'id_supplier',
			'id'    => 'id_supplier',
			'required' =>'required',
			'type'  => 'select',
			'value' => $this->form_validation->set_value('id_supplier'),
		);
		
			
			//print_r($this->data);exit;	
			$this->load->view('common/header', $meta);
			$this->load->view('purchase_order/add_order', $this->data);
			$this->load->view('common/footer');
	   
	}
	function get_productName()
	{
	
	
	$rate=$this->order_model->get_product($_REQUEST['code']);
	echo $rate; 
	
	
	}
		
   function view_order()
   {
   
	   $meta['page_title'] = "View Order";
	   $this->load->view('common/header', $meta);
	   $this->load->view('purchase_order/view_order');
       $this->load->view('common/footer');	
   
   
   }
   function get(){
		echo $this->order_model->get();
	}
	function delete_order($id)
    { 
    $this->order_model->delete($id);
     redirect("purchase_order/order/view_order");
    }	
	function edit_order($id){ 
		$user = $this->order_model->edit_order($id)->row();
		$products = $this->order_model->edit_products($id)->result();
		//print_r($user);exit;
		$suppliers = $this->order_model->get_supplier();
	
			$this->form_validation->set_rules('order_no', 'Order Number', 'required|xss_clean');
			$this->form_validation->set_rules('order_date', 'Order date', 'required|xss_clean');
			$this->form_validation->set_rules('expected_date', 'Expected date', 'required|xss_clean');
			$this->form_validation->set_rules('id_supplier', 'Supplier', 'required|xss_clean');
			$meta['page_title'] = "Edit Order";
			if (isset($_POST) && !empty($_POST)){
					$data = array(
						'order_no' => $this->input->post('order_no'),
						'id_supplier' => $this->input->post('id_supplier'),
						'order_date' => $this->input->post('order_date'),
						'expected_date' => $this->input->post('expected_date'),
						'status'   =>strtoupper('pending'),
			);
			
			if ($this->form_validation->run() === TRUE)
			{
				//print_r($data);exit;
					$this->order_model->update($data,$id);
					$name = $this->input->post('name');
					$company = $this->input->post('company');
					$category = $this->input->post('category');
					$quantity = $this->input->post('quantity');
					$length = count($name);
					for($i=0; $i<$length; $i++){
						$id_pur_product = $purchase_product_id[$i];
						$new_data['name'] = $name[$i];
						$new_data['company'] = $company[$i];
						$new_data['category'] = $category[$i];
						$new_data['quantity'] = $quantity[$i];
						$this->order_model->update_item($new_data,$id);
                    	}
						
									
				redirect("purchase_order/order/view_order");
			}
		}


		$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

		
 		
		$this->data['order_no'] = array(
			'name'  => 'order_no',
			'id'    => 'order_no',
			'type'  => 'text',
			'readonly'=> 'readonly',
			'value' => $this->form_validation->set_value('order_no',$user->order_no),
		);

		$this->data['order_date'] = array(
			'name'  => 'order_date',
			'id'    => 'order_date',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('date',$user->order_date),
			'class' => 'datepicker'
		);
		$this->data['expected_date'] = array(
			'name'  => 'expected_date',
			'id'    => 'expected_date',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('expected_date',$user->expected_date),
			'class' => 'datepicker'
		);


	$this->data['id_supplier'] = array(
			'name'  => 'id_supplier',
			'id'    => 'id_supplier',
			'type'  => 'select',
			'value' => $this->form_validation->set_value('id_supplier',$user->id_supplier),
		);
	
		$this->data['products'] = $products;
		$this->data['user'] = $user;
		$this->data['suppliers'] = $suppliers;
		//print_r($this->data);exit;
			$this->load->view('common/header', $meta);
			$this->load->view('purchase_order/edit_order', $this->data);
			$this->load->view('common/footer');
	
	
}
	
	
public function view_unique_order($id)
{
	$meta['page_title']="Order Details";
	$order = $this->order_model->get_orderno($id);
	$this->data['order_no'] = $order['order_no'];
	$this->data['supplier'] = $this->purchase_model->supplier_details($order['id_supplier']);
	$this->data['profile'] = $this->order_model->profile_details();
	$this->data['users']= $this->order_model->get_uniqueorder($id)->result();
	$this->load->view('common/header', $meta);
	$this->load->view('purchase_order/view_unique_order',$this->data);
	$this->load->view('common/footer');
}
	


public function approved_enquiry()
{
          if (!$this->ion_auth->logged_in())
			{
				redirect('auth', 'refresh');
			}
			if ( ! isset($_POST['approved']))
			{
		    redirect("enquiry/enquiry/view_enquiry");
			
			}
          $enquiry=$_REQUEST['enquiry_id'];
          $customer_enquiry=$_REQUEST['customer_enq_id'];
          $this->enquiry_model->status($enquiry);
          $user = $this->enquiry_model->get_enquiry($enquiry)->row();
          $this->data['taxes']=$this->enquiry_model->get_tax();
          $this->data['products']= $this->enquiry_model->get_all_product($enquiry)->result();
          $this->data['customers']=$this->enquiry_model->get_customer();
         $this->data['enquiry_no'] = array(
			'name'  => 'enquiry_no',
			'id'    => 'enquiry_no',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('enquiry_no',$user->customer_enquiry_no ),
		  );

		
		$this->data['view_date'] = array(
			'name'  => 'view_date',
			'id'    => 'view_date',
			'type'  => 'text',
		'data-date-format' =>'yyyy/mm/dd',
			'value' => $this->form_validation->set_value('view_date',$user->view_date),
			
		);
		
		$this->data['expected_date'] = array(
			'name'  => 'expected_date',
			'id'    => 'expected_date',
			'type'  => 'text',
			'data-date-format' =>'yyyy/mm/dd',
			'value' => $this->form_validation->set_value('expected_date',$user->expected_date),
		
		);

	  $this->data['id_customer'] = array(
			'name'  => 'id_customer',
			'id'    => 'id_customer',
			'type'  => 'select',
			'value' => $this->form_validation->set_value('id_customer',$user->customer_id),
		);


     $this->data['user'] = $user;
     $meta['page_title'] = "Enquiry Response";
     $this->load->view('common/header',$meta);
     $this->load->view('enquiry/enquiry_approved',$this->data);
     $this->load->view('common/footer');	

}	
public function sale_enquiry()
{
$id=$_REQUEST['hid_id'];
$enquiry_id=$_REQUEST['hidden_enquiry'];
  $this->data['customers']=$this->enquiry_model->get_customer();
   $user = $this->enquiry_model->get_enquiry($id)->row();
    $this->data['products']= $this->enquiry_model->get_all_product($id)->result();
if (!$this->ion_auth->logged_in())
			{
				redirect('auth', 'refresh');
			}

		$this->form_validation->set_rules('invoice_no', 'Invoice Number', 'required|xss_clean');
		$this->form_validation->set_rules('date', 'Bill date', 'required|xss_clean');
		
		
			
			$this->data['taxes']=$this->enquiry_model->get_tax();
			if($this->data['customers']==''){
				$this->data['customers']=array();
				}
			$id_branch=$this->session->userdata('id_branch');
	        $branch=$this->enquiry_model->get_invoicecode($id_branch)->row_array();
			$profile=$this->enquiry_model->get_profile()->row_array();
			$rate=$this->enquiry_model->get_invoiceno()->row_array();
			$r_n=$this->enquiry_model->get_invoiceno()->num_rows();
			$invoice=$this->enquiry_model->get_invoiceid()->row_array();
			if($r_n == 0){	
			$this->data['ic'] = $profile['invoice_code']."/".$branch['code']."/"."1";	
				}else{
					$new_id = $invoice['id']+1;
					$this->data['ic'] =$profile['invoice_code']."/".$branch['code']."/".$new_id;
					
				}
			
		if (isset($_POST['add']) && !empty($_POST))
		{  
		   
			if($this->input->post('trans_name')!=''){
				
				$trans_name = $this->input->post('trans_name');
				}else{
					$trans_name ='';
				}
				
			
			if($this->input->post('trans_amt')!=''){
				
				$trans_amt = $this->input->post('trans_amt');
				}else{
					$trans_amt =0;
				}
			$id_customer = $this->input->post('id_customer');
			if($id_customer!='0'){
				$data = array(
				'invoice_no' => $this->input->post('invoice_no'),
				'invoice_type' => $this->input->post('invoice_type'),
				'id_customer' => $id_customer,
				'customer_name' => strtoupper($this->enquiry_model->get_cname($id_customer)),
				'amount' => $this->input->post('total_amount'),
				'total_disc' => $this->input->post('total_discount'),
				'total_tax' => $this->input->post('total_tax'),
			
				'round_off' => $this->input->post('round_off'),
				'total_price' => $this->input->post('total_price'),
				'transaction_type' => $this->input->post('type'),
				'date' => $this->input->post('date'),
				'transport_name' => strtoupper($trans_name),	
				'transport_amt' => $trans_amt,
				'final_price' => $this->input->post('total_price'),
				'id_branch' => 	$id_branch,
				'billed_by' => 	$this->session->userdata('username')	
			
				);	
				
			}else{
				$data = array(
				'invoice_no' => $this->input->post('invoice_no'),
				'invoice_type' => $this->input->post('invoice_type'),
				'id_customer' => $this->input->post('id_customer'),
				'customer_name' => strtoupper($this->enquiry_model->get_cname($this->input->post('id_customer'))),
				'amount' => $this->input->post('total_amount'),
				'total_disc' => $this->input->post('total_discount'),
			
				'total_tax' => $this->input->post('total_tax'),
				'round_off' => $this->input->post('round_off'),
				'total_price' => $this->input->post('total_price'),
				'transaction_type' => $this->input->post('type'),
				'date' => $this->input->post('date'),
				'transport_name' => strtoupper($trans_name),	
				'transport_amt' => $trans_amt,
				'final_price' => $this->input->post('total_price'),
				'id_branch' => 	$id_branch,
				'billed_by' => strtoupper($this->session->userdata('username'))
			);
				
				
			} 
			//print_r($data);exit;
			if($data['transaction_type']==0){
				
				$data['status']=1;
			}
			
			if ($this->form_validation->run() === TRUE)
			{
				
				$latest_id=$this->enquiry_model->insert_sale($data,$user->id);
				$code = strtoupper($this->input->post('code'));
				$rate = $this->input->post('rate');
				$quantity = $this->input->post('quantity');
				$amount = $this->input->post('amount');
				
				$discount = $this->input->post('discount');
				$tax = $this->input->post('tax');
				$total = $this->input->post('total');
				//print_r($code);exit;
				$length = count($code);
					for($i=0; $i<$length; $i++){
						$new_data['date'] = $data['date'];
						$new_data['id_sale'] = $latest_id;
						$new_data['id_product'] =$this->enquiry_model->getID($code[$i]);
						$new_data['quantity'] = $quantity[$i];
						$new_data['rate'] = $rate[$i];
						$new_data['amount'] = $amount[$i];
						
						$new_data['tax'] = $tax[$i];	
						$new_data['discount'] = $discount[$i];
						$new_data['total'] = $total[$i];
						$new_data['ip']  =  $_SERVER['REMOTE_ADDR'];	
						//print_r($new_data);exit;
						//echo $id_branch;exit;
						$this->enquiry_model->insert_sale_item($new_data,$id_branch);	
					}							
				//echo "welldone prabhas!!";exit;
				redirect("sales/sales/view_sales");
			
				
				
			}
		}

		//display the edit user form
		//$this->data['csrf'] = $this->_get_csrf_nonce();

		//set the flash data error message if there is one
		$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

		
 		$in_v = $this->data['ic'];
		
		$this->data['invoice_no'] = array(
			'name'  => 'invoice_no',
			'id'    => 'invoice_no',
			'type'  => 'text',
			'value'=> $in_v,
			
		);
		$this->data['enquiry_no'] = array(
			'name'  => 'enquiry_no',
			'id'    => 'enquiry_no',
			'type'  => 'text',
			'value'=> $enquiry_id,
			
		);

		$this->data['date'] = array(
			'name'  => 'date',
			'id'    => 'date',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('date'),
			'class' => 'datepicker'
		);
		$this->data['hid_id'] = array(
			'name'  => 'hid_id',
			'id'    => 'hid_id',
			'type'  => 'hidden',
			'value' => $this->form_validation->set_value('hid_id',$user->id),
			
		);
		$this->data['hidden_enquiry'] = array(
			'name'  => 'hidden_enquiry',
			'id'    => 'hidden_enquiry',
			'type'  => 'hidden',
			'value' => $this->form_validation->set_value('hidden_enquiry',$user->customer_enquiry_no),
			
		);

	$this->data['id_customer'] = array(
			'name'  => 'id_customer',
			'id'    => 'id_customer',
			'type'  => 'select',
			'value' => $this->form_validation->set_value('id_customer',$user->customer_id),
		);
		      $this->data['user'] = $user;
			  $this->data['id']=$id;
			  $this->data['enquiry_id']=$enquiry_id;
			$this->data['taxes']=$this->enquiry_model->get_tax();
			$meta['page_title'] = "Add Enquiry Sales";
			$this->load->view('common/header', $meta);
			$this->load->view('enquiry/enquiry_sale', $this->data);
			$this->load->view('common/footer');
	   




}	
function search_quantity(){ 
		$id_branch=$this->session->userdata('id_branch');
		$rate=$this->enquiry_model->search_quantity($_REQUEST['code'], $_REQUEST['qty'],$id_branch);
		echo $rate;
		
	}	
}